import axios, { type AxiosRequestConfig, type AxiosResponse, type InternalAxiosRequestConfig } from "axios";
import { createDiscreteApi, lightTheme, type ConfigProviderProps } from "naive-ui";
const axiosInstance = axios.create({
    baseURL: "http://localhost:3000",
    timeout: 10000,
});

const { message } = createDiscreteApi(['message'], { configProviderProps: {} as ConfigProviderProps })
axiosInstance.interceptors.request.use(
    (config: InternalAxiosRequestConfig) => {
        return config;
    },
    (error) => {
        return Promise.reject(error);
    }
);

axiosInstance.interceptors.response.use(
    (response: AxiosResponse) => {
        if (response.data.code !== 200) {
            message.error(response.data.msg)
            return Promise.reject(new Error(response.data.msg))
        }
        return response.data;
    },
    (error) => {
        message.error(error.response.data.msg)
        return Promise.reject(error);
    }
);

export function request<T>(config: AxiosRequestConfig) {
    return axiosInstance<any, T>(config)
}